package com.in.mobilerechargedth.database;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import com.in.mobilerechargedth.constant.ConstantVariables;
import com.in.mobilerechargedth.model.ThirdScreenAdapterModel;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class FreeChargeDatasource {

	private SQLiteDatabase database;
	private MyDatabaseHelper dbHelper;

	public FreeChargeDatasource(Context context) {

		// Copy database from asset folder
		File dbFile = context.getDatabasePath(ConstantVariables.DATABASE_NAME);

		if (!dbFile.exists()) {
			copyDatabase(context);
		}

		dbHelper = new MyDatabaseHelper(context);
	}

	public void open() {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {

		dbHelper.close();
	}

	public List<String> getCircles() {
		List<String> circles = new ArrayList<String>();

		String sqlStr = "SELECT DISTINCT TRIM("
				+ ConstantVariables.COLUMN_CIRCLE + ") AS "
				+ ConstantVariables.COLUMN_CIRCLE + " FROM "
				+ ConstantVariables.TABLE_RECHARGE_PLAN + ";";
		Cursor cursor = database.rawQuery(sqlStr, null);

		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				String circle = cursor.getString(cursor
						.getColumnIndex(ConstantVariables.COLUMN_CIRCLE));
				circles.add(circle);
				cursor.moveToNext();
			}
		}

		cursor.close();
		return circles;
	}

	public List<String> getRechargePlans() {
		List<String> rechargePlan = new ArrayList<String>();

		String sqlStr = "SELECT DISTINCT TRIM("
				+ ConstantVariables.COLUMN_RECHARGE + ") AS "
				+ ConstantVariables.COLUMN_RECHARGE + " FROM "
				+ ConstantVariables.TABLE_RECHARGE_PLAN + ";";
		Cursor cursor = database.rawQuery(sqlStr, null);

		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				String plan = cursor.getString(cursor
						.getColumnIndex(ConstantVariables.COLUMN_RECHARGE));
				rechargePlan.add(plan);
				cursor.moveToNext();
			}
		}

		cursor.close();
		return rechargePlan;
	}

	public List<String> getAmounts(String recharge) {
		List<String> amounts = new ArrayList<String>();

		String sqlStr = "SELECT DISTINCT TRIM("
				+ ConstantVariables.COLUMN_AMOUNT + ") AS "
				+ ConstantVariables.COLUMN_AMOUNT + " FROM "
				+ ConstantVariables.TABLE_RECHARGE_PLAN + "" + " WHERE "
				+ ConstantVariables.COLUMN_RECHARGE + " = '" + recharge + "';";
		Cursor cursor = database.rawQuery(sqlStr, null);

		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				String amount = cursor.getString(cursor
						.getColumnIndex(ConstantVariables.COLUMN_AMOUNT));
				amounts.add(amount);
				cursor.moveToNext();
			}
		}

		cursor.close();
		return amounts;
	}

	public List<ThirdScreenAdapterModel> getRechargeDetails(String circle,
			String rechargePlan, String amount) {
		List<ThirdScreenAdapterModel> rechargeDetails = new ArrayList<ThirdScreenAdapterModel>();

		String sqlStr = "SELECT * FROM "
				+ ConstantVariables.TABLE_RECHARGE_PLAN + " WHERE "
				+ ConstantVariables.COLUMN_CIRCLE + " = '" + circle + "' AND "
				+ ConstantVariables.COLUMN_AMOUNT + " = '" + amount + "' AND "
				+ ConstantVariables.COLUMN_RECHARGE + " = '" + rechargePlan
				+ "';";

		Cursor cursor = database.rawQuery(sqlStr, null);

		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {

				ThirdScreenAdapterModel detail = new ThirdScreenAdapterModel();

				detail.setDetails(cursor
						.getString(
								cursor.getColumnIndex(ConstantVariables.COLUMN_OFFER_DETAILS))
						.trim());


				rechargeDetails.add(detail);

				cursor.moveToNext();
			}
		}
		cursor.close();
		return rechargeDetails;
	}

	private void copyDatabase(Context context) {
		InputStream mInput;
		try {
			mInput = context.getAssets().open(ConstantVariables.DATABASE_NAME);
			String outFileName = "data/data/" + context.getPackageName()
					+ "/databases/" + ConstantVariables.DATABASE_NAME;
			File mfile = new File(outFileName);
			if (!mfile.exists()) {
				File dir = new File("data/data/" + context.getPackageName()
						+ "/databases/");
				dir.mkdir();
				mfile.createNewFile();
			}

			OutputStream mOutput = new FileOutputStream(outFileName);
			byte[] buffer = new byte[1024];
			int length;
			while ((length = mInput.read(buffer)) > 0) {
				mOutput.write(buffer, 0, length);
			}

			mOutput.flush();
			mOutput.close();
			mInput.close();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}
}
